package cn.itcast.hotel;


import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

@SpringBootTest
public class HotelIndexTest {

     @Autowired
     private IHotelService iHotelService;

     RestHighLevelClient client = null;

    @BeforeEach
    public void init(){
        client = new RestHighLevelClient(
                RestClient.builder(
               HttpHost.create("http://192.168.72.132:9200")));
    }

    //新增
    @Test
   public void save() throws IOException {
        //查询一酒店数据
        Hotel hotel = iHotelService.getById(36934L);
        // 2、把hotel对象转成HotelDoc
        HotelDoc hotelDoc = new HotelDoc(hotel);
        //3.参考官网代码
        IndexRequest request = new IndexRequest("hotel");  //es索引库的名字
        request.id(hotel.getId().toString());
        String jsonString = JSON.toJSONString(hotelDoc);
        request.source(jsonString, XContentType.JSON);
        //4.发起请求
        IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(indexResponse));
    }
    //修改
    @Test
    void update() throws IOException {
        //查询一酒店数据
        Hotel hotel = iHotelService.getById(38665L);
        // 2、把hotel对象转成HotelDoc
        HotelDoc hotelDoc = new HotelDoc(hotel);
        //修改后的酒店名称
        hotelDoc.setName("修改后的酒店名称");
        //3.参考官网代码
        UpdateRequest request = new UpdateRequest("hotel", "38665");
        String jsonString = JSON.toJSONString(hotelDoc);
        request.doc(jsonString, XContentType.JSON);
        //4.发起请求
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(updateResponse));
    }

    //查询
    @Test
    void findById() throws IOException {
        GetRequest getRequest = new GetRequest("hotel", "38665");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
       // System.out.println(JSON.toJSONString(getResponse));
        System.out.println("---------------------------------------");
      //  System.out.println(getResponse.getSourceAsString());
        HotelDoc hotelDoc = JSON.parseObject(getResponse.getSourceAsString(), HotelDoc.class);
        System.out.println(hotelDoc);
    }
    //删除
    @Test
    void delete() throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("hotel", "38665");
        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);

    }

    //把tb_hotel表中的数据全部初始化到es中
    @Test
    void saveAll() throws IOException {
        //1.查询所有的酒店数据
        List<Hotel> hotelList = iHotelService.list();
        //2.创建一个批量请求的对象
        BulkRequest bulkRequest = new BulkRequest();
        //向bulkRequest中放入数据
        for (Hotel hotel : hotelList) {
            HotelDoc hotelDoc = new HotelDoc(hotel);
            bulkRequest.add(new IndexRequest("hotel").id(hotel.getId().toString()).source(JSON.toJSONString(hotelDoc),XContentType.JSON));
        }
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkResponse);
    }






    //创建索引库，一般不用java代码，用kibana
    @Test
    void createIndex() throws Exception {
        CreateIndexRequest request = new CreateIndexRequest("twitter");
        request.mapping(
                "{\n" +
                        "  \"properties\": {\n" +
                        "    \"message\": {\n" +
                        "      \"type\": \"text\"\n" +
                        "    }\n" +
                        "  }\n" +
                        "}",
                XContentType.JSON);
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);
    }


}
